/*
 * File      : gpio.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2015, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
 * 2015-03-24     Bright      the first version
 */

#include "stm32f4xx.h"
#include "gpio.h"
#include "board.h"

#include <rtdevice.h>


#ifdef RT_USING_PIN


#define STM32F4XX_PIN_NUMBERS 176 //STM32F429IGT6

#define __STM32_PIN(index, rcc, gpio, gpio_index) { 0, RCC_##rcc##ENR_GPIO##gpio##EN, GPIO##gpio, GPIO_PIN_##gpio_index}
#define __STM32_PIN_DEFAULT {-1, 0, 0, 0}

/* STM32 GPIO driver */
struct pin_index
{
    int index;
    uint32_t rcc;
    GPIO_TypeDef *gpio;
    uint32_t pin;
};

static const struct pin_index pins[] =
{
#if (STM32F4XX_PIN_NUMBERS == 176)
    __STM32_PIN_DEFAULT,
    __STM32_PIN(1, AHB1, E, 2),
    __STM32_PIN(2, AHB1, E, 3),
    __STM32_PIN(3, AHB1, E, 4),
    __STM32_PIN(4, AHB1, E, 5),
    __STM32_PIN(5, AHB1, E, 6),
    __STM32_PIN_DEFAULT,
    __STM32_PIN(7, AHB1, I, 8),
    __STM32_PIN(8, AHB1, C, 13),
    __STM32_PIN(9, AHB1, C, 14),
    __STM32_PIN(10, AHB1, C, 15),
    __STM32_PIN_DEFAULT,
    __STM32_PIN(12, AHB1, I, 10),
    __STM32_PIN(13, AHB1, I, 11),
    __STM32_PIN_DEFAULT,
    __STM32_PIN_DEFAULT,
    __STM32_PIN(16, AHB1, F, 0),
    __STM32_PIN(17, AHB1, F, 1),
    __STM32_PIN(18, AHB1, F, 2),
    __STM32_PIN(19, AHB1, F, 3),
    __STM32_PIN(20, AHB1, F, 4),
    __STM32_PIN(21, AHB1, F, 5),
    __STM32_PIN_DEFAULT,
    __STM32_PIN_DEFAULT,
    __STM32_PIN(24, AHB1, F, 6),
    __STM32_PIN(25, AHB1, F, 7),
    __STM32_PIN(26, AHB1, F, 8),
    __STM32_PIN(27, AHB1, F, 9),
    __STM32_PIN(28, AHB1, F, 10),
    __STM32_PIN(29, AHB1, H, 0),
    __STM32_PIN(30, AHB1, H, 1),
    __STM32_PIN_DEFAULT,
    __STM32_PIN(32, AHB1, C, 0),
    __STM32_PIN(33, AHB1, C, 1),
    __STM32_PIN(34, AHB1, C, 2),
    __STM32_PIN(35, AHB1, C, 3),
    __STM32_PIN_DEFAULT,
    __STM32_PIN_DEFAULT,
    __STM32_PIN_DEFAULT,
    __STM32_PIN_DEFAULT,
    __STM32_PIN(40, AHB1, A, 0),
    __STM32_PIN(41, AHB1, A, 1),
    __STM32_PIN(42, AHB1, A, 2),
    __STM32_PIN(43, AHB1, H, 2),
    __STM32_PIN(44, AHB1, H, 3),
    __STM32_PIN(45, AHB1, H, 4),
    __STM32_PIN(46, AHB1, H, 5),
    __STM32_PIN(47, AHB1, A, 3),
    __STM32_PIN_DEFAULT,
    __STM32_PIN_DEFAULT,
    __STM32_PIN(50, AHB1, A, 4),
    __STM32_PIN(51, AHB1, A, 5),
    __STM32_PIN(52, AHB1, A, 6),
    __STM32_PIN(53, AHB1, A, 7),
    __STM32_PIN(54, AHB1, C, 4),
    __STM32_PIN(55, AHB1, C, 5),
    __STM32_PIN(56, AHB1, B, 0),
    __STM32_PIN(57, AHB1, B, 1),
    __STM32_PIN(58, AHB1, B, 2),
    __STM32_PIN(59, AHB1, F, 11),
    __STM32_PIN(60, AHB1, F, 12),
    __STM32_PIN_DEFAULT,
    __STM32_PIN_DEFAULT,
    __STM32_PIN(63, AHB1, F, 13),
    __STM32_PIN(64, AHB1, F, 14),
    __STM32_PIN(65, AHB1, F, 15),
    __STM32_PIN(66, AHB1, G, 0),
    __STM32_PIN(67, AHB1, G, 1),
    __STM32_PIN(68, AHB1, E, 7),
    __STM32_PIN(69, AHB1, E, 8),
    __STM32_PIN(70, AHB1, E, 9),
    __STM32_PIN_DEFAULT,
    __STM32_PIN_DEFAULT,
    __STM32_PIN(73, AHB1, E, 10),
    __STM32_PIN(74, AHB1, E, 11),
    __STM32_PIN(75, AHB1, E, 12),
    __STM32_PIN(76, AHB1, E, 13),
    __STM32_PIN(77, AHB1, E, 14),
    __STM32_PIN(78, AHB1, E, 15),
    __STM32_PIN(79, AHB1, B, 10),
    __STM32_PIN(80, AHB1, B, 11),
    __STM32_PIN_DEFAULT,
    __STM32_PIN_DEFAULT,
    __STM32_PIN(83, AHB1, H, 6),
    __STM32_PIN(84, AHB1, H, 7),
    __STM32_PIN(85, AHB1, H, 8),
    __STM32_PIN(86, AHB1, H, 9),
    __STM32_PIN(87, AHB1, H, 10),
    __STM32_PIN(88, AHB1, H, 11),
    __STM32_PIN(89, AHB1, H, 12),
    __STM32_PIN_DEFAULT,
    __STM32_PIN_DEFAULT,
    __STM32_PIN(92, AHB1, B, 12),
    __STM32_PIN(93, AHB1, B, 13),
    __STM32_PIN(94, AHB1, B, 14),
    __STM32_PIN(95, AHB1, B, 15),
    __STM32_PIN(96, AHB1, D, 8),
    __STM32_PIN(97, AHB1, D, 9),
    __STM32_PIN(98, AHB1, D, 10),
    __STM32_PIN(99, AHB1, D, 11),
    __STM32_PIN(100, AHB1, D, 12),
    __STM32_PIN(101, AHB1, D, 13),
    __STM32_PIN_DEFAULT,
    __STM32_PIN_DEFAULT,
    __STM32_PIN(104, AHB1, D, 14),
    __STM32_PIN(105, AHB1, D, 15),
    __STM32_PIN(106, AHB1, G, 2),
    __STM32_PIN(107, AHB1, G, 3),
    __STM32_PIN(108, AHB1, G, 4),
    __STM32_PIN(109, AHB1, G, 5),
    __STM32_PIN(110, AHB1, G, 6),
    __STM32_PIN(111, AHB1, G, 7),
    __STM32_PIN(112, AHB1, G, 8),
    __STM32_PIN_DEFAULT,
    __STM32_PIN_DEFAULT,
    __STM32_PIN(115, AHB1, C, 6),
    __STM32_PIN(116, AHB1, C, 7),
    __STM32_PIN(117, AHB1, C, 8),
    __STM32_PIN(118, AHB1, C, 9),
    __STM32_PIN(119, AHB1, A, 8),
    __STM32_PIN(120, AHB1, A, 9),
    __STM32_PIN(121, AHB1, A, 10),
    __STM32_PIN(122, AHB1, A, 11),
    __STM32_PIN(123, AHB1, A, 12),
    __STM32_PIN(124, AHB1, A, 13),
    __STM32_PIN_DEFAULT,
    __STM32_PIN_DEFAULT,
    __STM32_PIN_DEFAULT,
    __STM32_PIN(128, AHB1, H, 13),
    __STM32_PIN(129, AHB1, H, 14),
    __STM32_PIN(130, AHB1, H, 15),
    __STM32_PIN(131, AHB1, I, 0),
    __STM32_PIN(132, AHB1, I, 1),
    __STM32_PIN(133, AHB1, I, 2),
    __STM32_PIN(134, AHB1, I, 3),
    __STM32_PIN_DEFAULT,
    __STM32_PIN_DEFAULT,
    __STM32_PIN(137, AHB1, A, 14),
    __STM32_PIN(138, AHB1, A, 15),
    __STM32_PIN(139, AHB1, C, 10),
    __STM32_PIN(140, AHB1, C, 11),
    __STM32_PIN(141, AHB1, C, 12),
    __STM32_PIN(142, AHB1, D, 0),
    __STM32_PIN(143, AHB1, D, 1),
    __STM32_PIN(144, AHB1, D, 2),
    __STM32_PIN(145, AHB1, D, 3),
    __STM32_PIN(146, AHB1, D, 4),
    __STM32_PIN(147, AHB1, D, 5),
    __STM32_PIN_DEFAULT,
    __STM32_PIN_DEFAULT,
    __STM32_PIN(150, AHB1, D, 6),
    __STM32_PIN(151, AHB1, D, 7),
    __STM32_PIN(152, AHB1, G, 9),
    __STM32_PIN(153, AHB1, G, 10),
    __STM32_PIN(154, AHB1, G, 11),
    __STM32_PIN(155, AHB1, G, 12),
    __STM32_PIN(156, AHB1, G, 13),
    __STM32_PIN(157, AHB1, G, 14),
    __STM32_PIN_DEFAULT,
    __STM32_PIN_DEFAULT,
    __STM32_PIN(160, AHB1, G, 15),
    __STM32_PIN(161, AHB1, B, 3),
    __STM32_PIN(162, AHB1, B, 4),
    __STM32_PIN(163, AHB1, B, 5),
    __STM32_PIN(164, AHB1, B, 6),
    __STM32_PIN(165, AHB1, B, 7),
    __STM32_PIN_DEFAULT,
    __STM32_PIN(167, AHB1, B, 8),
    __STM32_PIN(168, AHB1, B, 9),
    __STM32_PIN(169, AHB1, E, 0),
    __STM32_PIN(170, AHB1, E, 1),
    __STM32_PIN_DEFAULT,
    __STM32_PIN_DEFAULT,
    __STM32_PIN(173, AHB1, I, 4),
    __STM32_PIN(174, AHB1, I, 5),
    __STM32_PIN(175, AHB1, I, 6),
    __STM32_PIN(176, AHB1, I, 7),
#endif
};

struct pin_irq_map
{
    rt_uint32_t            irqpin;
    IRQn_Type              irqno;
};

static const  struct pin_irq_map pin_irq_map[] =
{
    {GPIO_PIN_0,   EXTI0_IRQn    },
    {GPIO_PIN_1,   EXTI1_IRQn    },
    {GPIO_PIN_2,   EXTI2_IRQn    },
    {GPIO_PIN_3,   EXTI3_IRQn    },
    {GPIO_PIN_4,   EXTI4_IRQn    },
    {GPIO_PIN_5,   EXTI9_5_IRQn  },
    {GPIO_PIN_6,   EXTI9_5_IRQn  },
    {GPIO_PIN_7,   EXTI9_5_IRQn  },
    {GPIO_PIN_8,   EXTI9_5_IRQn  },
    {GPIO_PIN_9,   EXTI9_5_IRQn  },
    {GPIO_PIN_10,  EXTI15_10_IRQn},
    {GPIO_PIN_11,  EXTI15_10_IRQn},
    {GPIO_PIN_12,  EXTI15_10_IRQn},
    {GPIO_PIN_13,  EXTI15_10_IRQn},
    {GPIO_PIN_14,  EXTI15_10_IRQn},
    {GPIO_PIN_15,  EXTI15_10_IRQn},
};

struct rt_pin_irq_hdr pin_irq_hdr_tab[] =
{
    {-1, 0, RT_NULL, RT_NULL},
    {-1, 0, RT_NULL, RT_NULL},
    {-1, 0, RT_NULL, RT_NULL},
    {-1, 0, RT_NULL, RT_NULL},
    {-1, 0, RT_NULL, RT_NULL},
    {-1, 0, RT_NULL, RT_NULL},
    {-1, 0, RT_NULL, RT_NULL},
    {-1, 0, RT_NULL, RT_NULL},
    {-1, 0, RT_NULL, RT_NULL},
    {-1, 0, RT_NULL, RT_NULL},
    {-1, 0, RT_NULL, RT_NULL},
    {-1, 0, RT_NULL, RT_NULL},
    {-1, 0, RT_NULL, RT_NULL},
    {-1, 0, RT_NULL, RT_NULL},
    {-1, 0, RT_NULL, RT_NULL},
    {-1, 0, RT_NULL, RT_NULL},
};

/* @brief : Calculate the number of pin pins*/
#define ITEM_NUM(items) sizeof(items)/sizeof(items[0])

/** @brief : Get the Pin information
  * @param PLLM: MCU physics pins (STM32F429IGT6->176)
* @retval : MCU Pin information
*/
const struct pin_index *get_pin(uint8_t pin)
{
    const struct pin_index *index;

    if (pin < ITEM_NUM(pins))
    {
        index = &pins[pin];
        if (index->index == -1)
            index = RT_NULL;
    }
    else
    {
        index = RT_NULL;
    }

    return index;
};
/**
 * @desc  : pin输入
 * @param : dev：设备结构体  pin:gpio引脚   value:写入的值
 * @return: None
 * @date  : 2017-8-6 13:37:18
 */
void stm32_pin_write(rt_device_t dev, rt_base_t pin, rt_base_t value)
{
    const struct pin_index *index;

    index = get_pin(pin);
    if (index == RT_NULL)
    {
        return;
    }

    if (value == PIN_LOW)
    {
        HAL_GPIO_WritePin(index->gpio, index->pin, GPIO_PIN_RESET);
    }
    else
    {
        HAL_GPIO_WritePin(index->gpio, index->pin, GPIO_PIN_SET);
    }
}
/**
 * @desc  : pin读取
 * @param : dev：设备结构体  pin:gpio引脚 
 * @return: 读出的值
 * @date  : 2017-8-6 13:39:28
 */
int stm32_pin_read(rt_device_t dev, rt_base_t pin)
{
    int value;
    const struct pin_index *index;

    value = PIN_LOW;

    index = get_pin(pin);
    if (index == RT_NULL)
    {
        return value;
    }

    if (HAL_GPIO_ReadPin(index->gpio, index->pin) == GPIO_PIN_RESET)
    {
        value = PIN_LOW;
    }
    else
    {
        value = PIN_HIGH;
    }

    return value;
}
/**
 * @desc  : pin 配置
 * @param : dev：设备结构体  pin:gpio引脚  mode:pin模式
 * @return: None
 * @date  : 2017-8-6 13:40:31 
 */
void stm32_pin_mode(rt_device_t dev, rt_base_t pin, rt_base_t mode)
{
    const struct pin_index *index;
    GPIO_InitTypeDef  GPIO_InitStructure;

    index = get_pin(pin);
    if (index == RT_NULL)
    {
        return;
    }

    /* GPIO Periph clock enable */
    SET_BIT(RCC->AHB1ENR, index->rcc);
    /* Configure GPIO_InitStructure */
    GPIO_InitStructure.Pin   = index->pin;
    GPIO_InitStructure.Speed = GPIO_SPEED_FAST;

    switch (mode)
    {
    case PIN_MODE_OUTPUT_NOPULL: /* 推挽输出*/
        GPIO_InitStructure.Pull = GPIO_NOPULL;
        GPIO_InitStructure.Mode  = GPIO_MODE_OUTPUT_PP;
        break;
    case PIN_MODE_INPUT_NOPULL:  /* 浮空输入*/
        GPIO_InitStructure.Pull = GPIO_NOPULL;
        GPIO_InitStructure.Mode  = GPIO_MODE_INPUT;
        break;
    case PIN_MODE_INPUT_PULLUP: /* 上拉输入*/
        GPIO_InitStructure.Pull = GPIO_PULLUP;
        GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
        break;
    case PIN_MODE_INPUT_PULLDOWN:/* 下拉输入*/
        GPIO_InitStructure.Pull = GPIO_PULLDOWN;
        GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
        break;
    case PIN_MODE_INPUT_ANALOG: /* 模拟输入*/
        GPIO_InitStructure.Pull = GPIO_MODE_ANALOG;
        GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
        break;
    default:
        break;
    }
    HAL_GPIO_Init(index->gpio, &GPIO_InitStructure);
}
/**
 * @desc  : 索引pin引脚号
 * @param : pin值
 * @return: pin对应的gpio通道
 * @date  : 2017-8-6 13:41:22
 */
rt_inline rt_int32_t bit2bitno(rt_uint32_t bit)
{
    int i;
    for(i = 0; i < 32; i++)
    {
        if((0x01 << i) == bit)
        {
            return i;
        }
    }
    return -1;
}
/**
 * @desc  : 匹配EXTI中断线
 * @param : pin通道
 * @return: Exti对应结构体
 * @date  : 2017年8月6日13:42:14
 */
rt_inline const struct pin_irq_map *get_pin_irq_map(uint32_t pinbit)
{
    rt_int32_t mapindex = bit2bitno(pinbit);
    if (mapindex < 0 || mapindex >= ITEM_NUM(pin_irq_map))
    {
        return RT_NULL;
    }
    return &pin_irq_map[mapindex];
};
/**
 * @desc  : 函数挂载到中断
 * @param : *device 驱动地址，pin:gpio通道，mode:gpio模式，hdr:钩子函数，args:钩子函数参数
 * @return: 执行状态
 * @date  : 2017-8-6 13:44:34
 */
rt_err_t stm32_pin_attach_irq(struct rt_device *device, rt_int32_t pin,rt_uint32_t mode, void (*hdr)(void *args), void *args)
{
    const struct pin_index *index;
    rt_base_t level;
    rt_int32_t irqindex = -1;

    index = get_pin(pin);
    if (index == RT_NULL)
    {
        return RT_ENOSYS;
    }
    irqindex = bit2bitno(index->pin);
    if (irqindex < 0 || irqindex >= ITEM_NUM(pin_irq_map))
    {
        return RT_ENOSYS;
    }

    level = rt_hw_interrupt_disable();
		/* 配置信息已经存在,返回ok*/
    if (pin_irq_hdr_tab[irqindex].pin == pin   &&
            pin_irq_hdr_tab[irqindex].hdr == hdr   &&
            pin_irq_hdr_tab[irqindex].mode == mode &&
            pin_irq_hdr_tab[irqindex].args == args
       )
    {
        rt_hw_interrupt_enable(level);
        return RT_EOK;
    }
    if (pin_irq_hdr_tab[irqindex].pin != -1)
    {
        rt_hw_interrupt_enable(level);
        return RT_EBUSY;
    }
    pin_irq_hdr_tab[irqindex].pin = pin;
    pin_irq_hdr_tab[irqindex].hdr = hdr;
    pin_irq_hdr_tab[irqindex].mode = mode;
    pin_irq_hdr_tab[irqindex].args = args;
    rt_hw_interrupt_enable(level);

    return RT_EOK;
}
/**
 * @desc  : 函数从中断卸载
 * @param : *device 驱动地址，pin:gpio通道，mode:gpio模式，hdr:钩子函数，args:钩子函数参数
 * @return: 执行状态
 * @date  : 2017-8-6 13:45:09
 */
rt_err_t stm32_pin_dettach_irq(struct rt_device *device, rt_int32_t pin)
{
    const struct pin_index *index;
    rt_base_t level;
    rt_int32_t irqindex = -1;

    index = get_pin(pin);
    if (index == RT_NULL)
    {
        return RT_ENOSYS;
    }
    irqindex = bit2bitno(index->pin);
    if (irqindex < 0 || irqindex >= ITEM_NUM(pin_irq_map))
    {
        return RT_ENOSYS;
    }

    level = rt_hw_interrupt_disable();
    if (pin_irq_hdr_tab[irqindex].pin == -1)
    {
        rt_hw_interrupt_enable(level);
        return RT_EOK;
    }
    pin_irq_hdr_tab[irqindex].pin = -1;
    pin_irq_hdr_tab[irqindex].hdr = RT_NULL;
    pin_irq_hdr_tab[irqindex].mode = 0;
    pin_irq_hdr_tab[irqindex].args = RT_NULL;
    rt_hw_interrupt_enable(level);

    return RT_EOK;
}
/**
 * @desc  : 开启中断
 * @param : *device 驱动地址，pin:gpio通道，enable:开关
 * @return: 执行状态
 * @date  : 2017-8-6 13:45:13
 */
rt_err_t stm32_pin_irq_enable(struct rt_device *device, rt_base_t pin, rt_uint32_t enabled)
{
    const struct pin_index *index;
    const struct pin_irq_map *irqmap;
    rt_base_t level;
    rt_int32_t irqindex = -1;
    GPIO_InitTypeDef  GPIO_InitStructure;

    index = get_pin(pin);
    if (index == RT_NULL)
    {
        return RT_ENOSYS;
    }
    if (enabled == PIN_IRQ_ENABLE)
    {
        irqindex = bit2bitno(index->pin);
        if (irqindex < 0 || irqindex >= ITEM_NUM(pin_irq_map))
        {
            return RT_ENOSYS;
        }
        level = rt_hw_interrupt_disable();
        if (pin_irq_hdr_tab[irqindex].pin == -1)
        {
            rt_hw_interrupt_enable(level);
            return RT_ENOSYS;
        }
        irqmap = &pin_irq_map[irqindex];
        /* GPIO Periph clock enable */
        SET_BIT(RCC->AHB1ENR, index->rcc);

        /* Configure GPIO_InitStructure */
        GPIO_InitStructure.Pin   = index->pin;
        GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
        GPIO_InitStructure.Pull = GPIO_NOPULL;

        switch (pin_irq_hdr_tab[irqindex].mode)
        {
        case PIN_IRQ_MODE_RISING:
            GPIO_InitStructure.Mode  = GPIO_MODE_IT_RISING;
            break;
        case PIN_IRQ_MODE_FALLING:
            GPIO_InitStructure.Mode  = GPIO_MODE_IT_FALLING;
            break;
        case PIN_IRQ_MODE_RISING_FALLING:
            GPIO_InitStructure.Mode  = GPIO_MODE_IT_RISING_FALLING;
            break;
        }
        HAL_GPIO_Init(index->gpio, &GPIO_InitStructure);
        rt_hw_interrupt_enable(level);
        /* EXTI interrupt init*/
        HAL_NVIC_SetPriority(irqmap->irqno, 1, 0);
        HAL_NVIC_EnableIRQ(irqmap->irqno);
    }
    else if (enabled == PIN_IRQ_DISABLE)
    {
        irqmap = get_pin_irq_map(index->pin);
        if (irqmap == RT_NULL)
        {
            return RT_ENOSYS;
        }
        GPIO_InitStructure.Pin   = index->pin;
        GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
        GPIO_InitStructure.Pull = GPIO_NOPULL;
        GPIO_InitStructure.Mode  = GPIO_MODE_ANALOG;
        HAL_GPIO_Init(index->gpio, &GPIO_InitStructure);
        HAL_NVIC_DisableIRQ(irqmap->irqno);
    }
    else
    {
        return RT_ENOSYS;
    }
    return RT_EOK;
}

const static struct rt_pin_ops _stm32_pin_ops =
{
    stm32_pin_mode,
    stm32_pin_write,
    stm32_pin_read,
    stm32_pin_attach_irq,
    stm32_pin_dettach_irq,
    stm32_pin_irq_enable,
};

int stm32_hw_pin_init(void)
{
    int result;

    result = rt_device_pin_register("pin", &_stm32_pin_ops, RT_NULL);
    return result;
}
INIT_BOARD_EXPORT(stm32_hw_pin_init);

rt_inline void pin_irq_hdr(int irqno)
{
	__HAL_GPIO_EXTI_CLEAR_IT(pin_irq_map[irqno].irqpin);
    if (pin_irq_hdr_tab[irqno].hdr)
    {
        pin_irq_hdr_tab[irqno].hdr(pin_irq_hdr_tab[irqno].args);
    }
}

void EXTI0_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();
    pin_irq_hdr(0);
    /* leave interrupt */
    rt_interrupt_leave();
}
void EXTI1_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();
    pin_irq_hdr(1);
    /* leave interrupt */
    rt_interrupt_leave();
}
void EXTI2_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();
    pin_irq_hdr(2);
    /* leave interrupt */
    rt_interrupt_leave();
}
void EXTI3_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();
    pin_irq_hdr(3);
    /* leave interrupt */
    rt_interrupt_leave();
}
void EXTI4_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();
    pin_irq_hdr(4);
    /* leave interrupt */
    rt_interrupt_leave();
}
void EXTI9_5_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();
	  if(__HAL_GPIO_EXTI_GET_IT(pin_irq_map[5].irqno) != RESET) 
    {
        pin_irq_hdr(5);
    }
    if(__HAL_GPIO_EXTI_GET_IT(pin_irq_map[6].irqno) != RESET) 
    {
        pin_irq_hdr(6);
    }
    if(__HAL_GPIO_EXTI_GET_IT(pin_irq_map[7].irqno) != RESET) 
    {
        pin_irq_hdr(7);
    }
    if(__HAL_GPIO_EXTI_GET_IT(pin_irq_map[8].irqno) != RESET) 
    {
        pin_irq_hdr(8);
    }
    if(__HAL_GPIO_EXTI_GET_IT(pin_irq_map[9].irqno) != RESET) 
    {
        pin_irq_hdr(9);
    }
    /* leave interrupt */
    rt_interrupt_leave();
}
void EXTI15_10_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();
    if(__HAL_GPIO_EXTI_GET_IT(pin_irq_map[10].irqno) != RESET) 
    {
        pin_irq_hdr(10);
    }
    if(__HAL_GPIO_EXTI_GET_IT(pin_irq_map[11].irqno) != RESET) 
    {
        pin_irq_hdr(11);
    }
    if(__HAL_GPIO_EXTI_GET_IT(pin_irq_map[12].irqno) != RESET) 
    {
        pin_irq_hdr(12);
    }
    if(__HAL_GPIO_EXTI_GET_IT(pin_irq_map[13].irqno) != RESET) 
    {
        pin_irq_hdr(13);
    }
    if(__HAL_GPIO_EXTI_GET_IT(pin_irq_map[14].irqno) != RESET) 
    {
        pin_irq_hdr(14);
    }
    if(__HAL_GPIO_EXTI_GET_IT(pin_irq_map[15].irqno) != RESET) 
    {
        pin_irq_hdr(15);
    }
    /* leave interrupt */
    rt_interrupt_leave();
}
#endif
